package com.keyou.reggie.common;

import com.keyou.reggie.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @Author: klq
 * @Date: 2023/1/9 18:33
 * @Description: 统一结果封装类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultInfo<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    // 编码：1成功，0和其他数字失败
    private Integer code;

    //错误信息
    private String msg;

    //数据
    private T data;

    //动态数据
    private HashMap<String, Object> map = new HashMap<>();


    public static <T> ResultInfo<T> success(T data) {
        ResultInfo<T> r = new ResultInfo<>();
        r.code = ResultCode.RESULT_SUCESS.getCode();
        r.data = data; //返回接口数据
        return r;
    }

    public static <T> ResultInfo<T> success(T data, String msg) {
        ResultInfo<T> r = new ResultInfo<>();
        r.code = ResultCode.RESULT_SUCESS.getCode();
        r.data = data; //返回接口数据
        r.msg = msg; //设置返回信息
        return r;
    }

    public static <T> ResultInfo<T> error(String errMsg) {
        ResultInfo<T> r = new ResultInfo<>();
        r.msg = errMsg; //设置错误信息
        r.code = ResultCode.RESULT_ERROR.getCode();
        return r;
    }

    public static <T> ResultInfo<T> systemError() {
        ResultInfo<T> r = new ResultInfo<>();
        r.msg = ResultCode.RESULT_SYSTEM_ERROR.getMsg();
        r.code = ResultCode.RESULT_SYSTEM_ERROR.getCode();
        return r;
    }

    public ResultInfo<T> add(String msg, Object value) {
        this.map.put(msg, value);
        return this;
    }

}